package com.shangyueshang.request;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.utils.ParamsTool;
import com.shangyueshang.utils.SecurityUtil;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 16:52
 * @version: v1.0.0
 */
@Data
public class PageReq {
	/**
	 * 页码
	 */
	private int pageNum = 1;
	/**
	 * 页数
	 */
	private int pageSize = 10;
	/**
	 * 查询条件
	 */
	private Map<String, Object> params = new HashMap<>();
	
	/**
	 * 驼峰参数转下划线
	 *
	 * @return
	 */
	@JsonIgnore
	public Map<String, Object> getLineParams() {
		List<String> shops = SecurityUtil.getAuthorizedShops();
		Map<String, Object> params2 = new HashMap<>(params.size());
		params.forEach(
				(k, v) -> {
					if (k.equals(SysConstants.shop)) {
						if (shops.contains(v.toString())) {
							params2.put(SysConstants.shop, Collections.singletonList(v.toString()));
						} else {
							params2.put(SysConstants.shop, shops);
						}
					} else if (Objects.nonNull(v) && StringUtils.hasText(v.toString())) {
						params2.put(ParamsTool.humpToLine(k), v);
					}
				}
		);
	
		return params2;
	}
}
